/**
 * ===========================================================
 * 文件名：Device.java
 * 所在包：com.innofidei.android.mobileoa.core.util
 * 功能说明：设备基本信息类
 * 创建人：huangbo
 * 创建时间：2012-9-26
 * Copyright (c) 2012 Innofidei Inc All rights reserved.
 * ===========================================================
 */
package com.njrsrc.common.utils;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

import com.njrsrc.common.Setting;

/**
 * 设备基本信息
 * @author Oliverzhu
 * 2013/4/14
 */
public class Device {

	//the screen pixel
	public static int screenWidth;
	public static int screenHeight;
	
	//lbs
	public static double latitude;
	public static double longitude;
	
	// reference android.telephony.TelephonyManager
	public static String deviceId; // device id
	public static String deviceSoftwareVersion;
	public static String line1Number;
	public static String networkCountryIso;
	public static String networkOperator;
	public static String networkOperatorName;
	public static String simCountryIso;
	public static String simOperator;
	public static String simOperatorName;
	public static String simSerialNumber;
	public static String subscriberId;

	public static CdmaCellLocation cdmaCellLocation;
	public static GsmCellLocation gsmCellLocation;

	public static String versionName;

	// reference android.os.Build
	public static String board;
	public static String bootloader;
	public static String brand;
	public static String cpuABI;
	public static String cpuABI2;
	public static String device;
	public static String display;
	public static String fingerPrint;
	public static String hardware;
	public static String host;
	public static String id;
	public static String manufacturer;
	public static String model;
	public static String product;
	public static String radio;
	public static String serial;
	public static String tags;
	public static long time;
	public static String type;
	public static String user;

	// reference android.os.android.Build.VERSION
	public static String codeName;
	public static String incremental;
	public static String release;
	public static String sdk;
	public static int sdkInt;

	/**
	 * init the device's info
	 */
	public static void init(Context context) {
		// telephony info
		TelephonyManager phoneManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		deviceId = phoneManager.getDeviceId(); // device id
		deviceSoftwareVersion = phoneManager.getDeviceSoftwareVersion();
		line1Number = phoneManager.getLine1Number();
		networkCountryIso = phoneManager.getNetworkCountryIso();
		networkOperator = phoneManager.getNetworkOperator();
		networkOperatorName = phoneManager.getNetworkOperatorName();
		simCountryIso = phoneManager.getSimCountryIso();
		simOperator = phoneManager.getSimOperator();
		simOperatorName = phoneManager.getSimOperatorName();
		simSerialNumber = phoneManager.getSimSerialNumber();
		subscriberId = phoneManager.getSubscriberId();

		CellLocation cellLocation = phoneManager.getCellLocation();
		if (cellLocation instanceof CdmaCellLocation) {
			cdmaCellLocation = (CdmaCellLocation) cellLocation;
		} else if (cellLocation instanceof GsmCellLocation) {
			gsmCellLocation = (GsmCellLocation) cellLocation;
		}

		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			versionName = pi.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// version values
		codeName = Build.VERSION.CODENAME;
		incremental = Build.VERSION.INCREMENTAL;
		release = Build.VERSION.RELEASE;
		sdk = Build.VERSION.SDK;
		sdkInt = Build.VERSION.SDK_INT;

		// build values
		board = Build.BOARD;
		// bootloader = Build.BOOTLOADER;
		brand = Build.BRAND;
		cpuABI = Build.CPU_ABI;
		// cpuABI2 = Build.CPU_ABI2;
		device = Build.DEVICE;
		display = Build.DISPLAY;
		fingerPrint = Build.FINGERPRINT;
		// hardware = Build.HARDWARE;
		host = Build.HOST;
		id = Build.ID;
		manufacturer = Build.MANUFACTURER;
		model = Build.MODEL;
		product = Build.PRODUCT;
		// radio = Build.RADIO;
		// serial;
		tags = Build.TAGS;
		time = Build.TIME;
		type = Build.TYPE;
		user = Build.USER;
	}

	/**
	 * 得到当前所在的城市
	 * @return
	 */
	public static String getCity(Context context) {
		String city = null;
		try {
			LocationManager locationManager;
			String serviceName = Context.LOCATION_SERVICE;
			locationManager = (LocationManager) ((Activity) context)
					.getSystemService(serviceName);
			String provider = LocationManager.NETWORK_PROVIDER;
			Criteria criteria = new Criteria();
			criteria.setAccuracy(Criteria.ACCURACY_FINE);
			criteria.setAltitudeRequired(false);
			criteria.setBearingRequired(false);
			criteria.setCostAllowed(true);
			criteria.setPowerRequirement(Criteria.POWER_LOW);

			Location location1 = locationManager.getLastKnownLocation(provider);

			city = updateWithNewLocation(location1, context);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Looper.prepare();
		// locationManager.requestLocationUpdates(provider, 2000, 10,
		// locationListener);
		// Looper.loop();
		return city;
	}

	private static String updateWithNewLocation(Location location,
			Context context) {
		String latLongString = null;
		if (location != null) {

			// latLongString = "纬度:" + lat + "\n经度:" + lng;
		} else {
			latLongString = "无法获取地理信息";
			return latLongString;
		}
		List<Address> addList = null;
		Geocoder ge = new Geocoder(context);
		try {
			double latitude = location.getLatitude() * 1E6;
			double longitude = location.getLongitude() * 1E6;

			double lat = (int) latitude / 1E6;
			double lng = (int) longitude / 1E6;
			addList = ge.getFromLocation(lat, lng, 1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (addList != null && addList.size() > 0) {

			Address ad = addList.get(0);
			latLongString = ad.getLocality();

		}
		
		if(Setting.DEBUG) Log.i("Devices", "您当前的位置是: " + latLongString);

		return latLongString;
	}
}
